﻿<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增组织</title>
    <jsp:include page="/resources/inc/formHead.jsp"/>
</head>
<body>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6">
            <div class="layui-card">
                <div class="layui-card-header">增权限</div>
                <div class="layui-card-body">
                    <ul id="ztree1" class="ztree"></ul>
                </div>
            </div>
        </div>
        <div class="layui-col-md6">
            <div class="layui-card">
                <div class="layui-card-header">减权限</div>
                <div class="layui-card-body">
                    <ul id="ztree2" class="ztree"></ul>
                </div>
            </div>
        </div>
        <div class="layui-btn-group">
            <button class="layui-btn" onclick="permissionSubmit()">保存</button>
            <button class="layui-btn" onclick="closeDialog()">取消</button>
        </div>
    </div>
</div>
<jsp:include page="/resources/inc/formFooter.jsp"/>
<script>
    var addSystems = [];
    var addPermissions = [];
    var setting1 = {
        check: {
            enable: true,
            // 勾选关联父，取消关联子
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        async: {
            enable: true,
            url: '${basePath}/manage/permission/user/${user.userId}?type=1'
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: function () {
                var zTree = $.fn.zTree.getZTreeObj("ztree1");
                var changeNodes = zTree.getChangeCheckedNodes();
                addSystems = [];
                addPermissions = [];
                $(changeNodes).each(function (index, node) {
                    if (node.isSystem) {
                        addSystems.push({
                            id: node.realId,
                            checked: node.checked,
                            type: 1
                        });
                    } else {
                        addPermissions.push({
                            id: node.realId,
                            checked: node.checked,
                            type: 1
                        });
                    }
                });
            }
        }
    };
    var deleteSystem = [];
    var deletePermission = [];
    var setting2 = {
        check: {
            enable: true,
            // 勾选关联父，取消关联子
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        async: {
            enable: true,
            url: '${basePath}/manage/permission/user/${user.userId}?type=-1'
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: function () {
                var zTree = $.fn.zTree.getZTreeObj("ztree2")
                var changeNodes = zTree.getChangeCheckedNodes();
                deleteSystem = [];
                deletePermission = [];
                $(changeNodes).each(function (index, node) {
                    if (node.isSystem) {
                        deleteSystem.push({
                            id: node.realId,
                            checked: node.checked,
                            type: -1
                        });
                    } else {
                        deletePermission.push({
                            id: node.realId,
                            checked: node.checked,
                            type: -1
                        });
                    }
                });
            }
        }
    };
    function initTree() {
        $.fn.zTree.init($('#ztree1'), setting1);
        $.fn.zTree.init($('#ztree2'), setting2);
    }

    function permissionSubmit() {
        // 合并提交
        var systems = addSystems.concat(deleteSystem);
        var permissions = addPermissions.concat(deletePermission);
        $.ajax({
            type: 'post',
            url: '${basePath}/manage/user/permission/${user.userId}',
            data: {systems: JSON.stringify(systems), permissions: JSON.stringify(permissions)},
            success: function (result) {
                if (result.code != 1) {
                    if (result.data instanceof Array) {
                        $.each(result.data, function (index, value) {
                            layer.msg(value.errorMsg);
                        });
                    } else if (typeof(result.data) != "undefined" && result.data.length > 0) {
                        layer.msg(result.data, {icon: 5});
                    } else {
                        layer.msg(result.message, {icon: 5});
                    }
                } else {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    parent.$table.bootstrapTable('refresh');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg(textStatus, {icon: 5});
            }
        });
    }
    function closeDialog() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

    $(function () {
        initTree();
    })
</script>
</body>
</html>
